package cn.hnshxy.dch;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/jielong")
public class JieLong extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		String cy = request.getParameter("chengyu");
		String time=request.getParameter("time");
		String history = (String) session.getAttribute("history");
		String msg=null;
		String first=(String) session.getAttribute("first");//获得人接龙的成语首字
		boolean isUserWin=false;
		if(cy!=null) {//提交过来的
			cy=cy.trim();
			try {
				isUserWin=(boolean)session.getAttribute("isUserWin");
				if(isUserWin)throw new Exception("不用刷新，你是最棒的~");
				if(time==null||time.isEmpty())throw new Exception("检测到非法操作~");
				if(((String)session.getAttribute("time")).equals(time))throw new Exception("请不要刷新页面~");
				if(cy.isEmpty())throw new Exception("请输入成语~");
				if(cy.length()<3||cy.length()>14)throw new Exception("成语应该在3到14个汉字范围内~");
				if(first!=null && !cy.startsWith(first))throw new Exception("请输入以“"+first+"”字开头的成语");
				first=null;
				DbHelper db = new DbHelper();
				List<Map<String, Object>> list = db.executeQuery("select cy from chengyu where cy=?", cy);
				if(list==null)throw new Exception(db.getErrorMessage());
				if(list.size()==0) {//数据库中找不到你输入的成语，即你输入的cy不是成语
					msg="您输入的“"+cy+"”不是成语~";
					if(!history.isEmpty())first=cy.substring(0, 1);//如果history中有记录则将first改为成语的首字，即first不不变
				}else {
					history+=","+cy;
					list=db.executeQuery("select cy from chengyu where first=? order by rand() limit 1", cy.substring(cy.length()-1));
					if(list==null)throw new Exception(db.getErrorMessage());
					if(list.size()==0) {
						msg="恭喜，你在本轮战胜了计算机~";
						isUserWin=true;
					}else {
						cy=(String)list.get(0).get("cy");
						history+=","+cy;
						first=cy.substring(cy.length()-1);
					}
				}
			}catch (Exception e) {
				msg=e.getMessage();
			}
		}else {
			first=null;
			history="";
			time="0";
		}
//		history=",一清二白,白手起家,家财万贯,贯护士了,了话还是";
//		msg="<script>alert('haha')</script>您输入的‘1234’不是成语~";
		session.setAttribute("first", first);
		request.setAttribute("msg", msg);
		session.setAttribute("history", history);
		session.setAttribute("time", time);
		session.setAttribute("isUserWin", isUserWin);
		request.getRequestDispatcher("/v2/index.jsp").forward(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
